﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CodeGeneratorHelper;
using CodeGeneratorHelper.DAL;
using CodeGeneratorHelper.Entities;

namespace CodeGeneratorUtility
{
    public partial class frmCodeGenerator : Form
    {
        public frmCodeGenerator()
        {
            InitializeComponent();
        }

        private void btnLoadTables_Click(object sender, EventArgs e)
        {
            LoadTables(cboTables.Text);
        }
        private void tbAutoSearch_TextChanged(object sender, EventArgs e)
        {
            try
            {
                LoadTables(tbAutoSearch.Text);
            }
            catch (System.Exception ex)
            {
                MessageBox.Show("Error :" + ex.Message);
            }
        }
        private void LoadTables(string tableName)
        {
            try
            {
                DataSet ds = DBHelper.ExecuteDatasetQuery("SELECT name FROM sys.Tables Where name like '"+tableName+"%'");
                List<string> lstTables = new List<string>();
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    lstTables.Add(ds.Tables[0].Rows[i][0].ToString());
                }
                cboTables.DataSource = lstTables;
            }
            catch (System.Exception ex)
            {
                MessageBox.Show("Error :" + ex.Message);
            }
        }

        
        private void btnGenerateEntity_Click(object sender, EventArgs e)
        {
            try
            {
                List<ColumnInfo> lstColumns = DBHelper.GetColumnsInfo(cboTables.Text);
                tbCode.Text = CodeBuilder.GetEntityCode(lstColumns, cboTables.Text);
            }
            catch (System.Exception ex)
            {
                MessageBox.Show("Error :" + ex.Message);
            }
        }

        private void btnSPParams_Click(object sender, EventArgs e)
        {
            try
            {
                List<ColumnInfo> lstColumns = DBHelper.GetColumnsInfo(cboTables.Text);
                tbCode.Text = CodeBuilder.GetSPParameters(lstColumns, cboTables.Text);
            }
            catch (System.Exception ex)
            {
                MessageBox.Show("Error :" + ex.Message);
            }
        }

        private void btnSPInsert_Click(object sender, EventArgs e)
        {
            try
            {
                List<ColumnInfo> lstColumns = DBHelper.GetColumnsInfo(cboTables.Text);
                tbCode.Text = CodeBuilder.GetInsertScript(lstColumns, cboTables.Text);
            }
            catch (System.Exception ex)
            {
                MessageBox.Show("Error :" + ex.Message);
            }
        }

        private void btnSPUpdate_Click(object sender, EventArgs e)
        {
            try
            {
                List<ColumnInfo> lstColumns = DBHelper.GetColumnsInfo(cboTables.Text);
                tbCode.Text = CodeBuilder.GetUpdateScript(lstColumns, cboTables.Text);
            }
            catch (System.Exception ex)
            {
                MessageBox.Show("Error :" + ex.Message);
            }
        }

        private void btnLoadEntity_Click(object sender, EventArgs e)
        {
            try
            {
                List<ColumnInfo> lstColumns = DBHelper.GetColumnsInfo(cboTables.Text);
                tbCode.Text = CodeBuilder.GetLoadEntity(lstColumns, cboTables.Text);
            }
            catch (System.Exception ex)
            {
                MessageBox.Show("Error :" + ex.Message);
            }
        }

        private void btnCodeSPParams_Click(object sender, EventArgs e)
        {
            try
            {
                List<ColumnInfo> lstColumns = DBHelper.GetColumnsInfo(cboTables.Text);
                tbCode.Text = CodeBuilder.GetInputParameter(lstColumns, cboTables.Text);
            }
            catch (System.Exception ex)
            {
                MessageBox.Show("Error :" + ex.Message);
            }
        }

        private void btnGetMigrationXML_Click(object sender, EventArgs e)
        {
            try
            {
                List<ColumnInfo> lstColumns = DBHelper.GetColumnsInfo(cboTables.Text);
                tbCode.Text = CodeBuilder.GetMigrationXML(lstColumns, cboTables.Text);
            }
            catch (System.Exception ex)
            {
                MessageBox.Show("Error :" + ex.Message);
            }
        }

        private void btnECNEntity_Click(object sender, EventArgs e)
        {
            try
            {
                List<ColumnInfo> lstColumns = DBHelper.GetColumnsInfo(cboTables.Text);
                tbCode.Text = CodeBuilder.GetEntityCode(lstColumns, cboTables.Text,true);
            }
            catch (System.Exception ex)
            {
                MessageBox.Show("Error :" + ex.Message);
            }
        }

        private void btnECNSPParams_Click(object sender, EventArgs e)
        {
            try
            {
                List<ColumnInfo> lstColumns = DBHelper.GetColumnsInfo(cboTables.Text);
                tbCode.Text = CodeBuilder.GetECNParameters(lstColumns, cboTables.Text);
            }
            catch (System.Exception ex)
            {
                MessageBox.Show("Error :" + ex.Message);
            }
        }

        private void btnLoadECNEntity_Click(object sender, EventArgs e)
        {
            try
            {
                List<ColumnInfo> lstColumns = DBHelper.GetColumnsInfo(cboTables.Text);
                tbCode.Text = CodeBuilder.GetLoadECNEntity(lstColumns, cboTables.Text);
            }
            catch (System.Exception ex)
            {
                MessageBox.Show("Error :" + ex.Message);
            }
        }
        
       
    }
}
